package team.hyznrj.studentsys.entity;
/**
 * Excel实体类
 * @author 郑泽锋
 * @date 2018年5月2日
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import lombok.ToString;

/**
 * Excel配置表，配置Excel的单元属性、字体等
 * @author 郑泽锋
 * @date 2018年5月3日
 */
@ToString
public class ExcelEntity {
	public ExcelEntity() {
		super();
	}

	/**
	 * 创造者
	 */
	private String flag;
	/**
	 * Excel的全部内容
	 */
	private List<ExcelUnitObject> content;
	/**
	 * Excel的备注
	 */
	private List<ExcelRemarkObject> remarks;
	/**
	 * 宽的最大值
	 */
	private int maxWidth;
	/**
	 * 高的最大值
	 */
	private int maxHeigh;
	/**
	 * 最大长(像素)
	 */
	private double maxWidthLength;
	/**
	 * 最大高(像素)
	 */
	private double maxHeighLength;
	/**
	 * 工作簿名
	 */
	private String sheetName;
	/**
	 * 导出表名
	 */
	private String fileName;
	/**
	 * 自适应行/列高设置: Key:行/列号 Value:字符串数量 公式:字符串占n行*字符数*2*256
	 */
	private Map<Integer, Double> autuHeightSettingMap;
	private Map<Integer, Double> autuWidthSettingMap;
	/**
	 * 合并单元格 高度换算 首格高度占比
	 * 数值越大，越依靠首格长度
	 * 综测默认为0.25
	 * 成绩表应为0.65
	 */
	private double rate=0.5;
	public ExcelEntity(String flag) {
		this.remarks = new ArrayList<>();
		this.content = new ArrayList<>();
		this.autuHeightSettingMap = new HashMap<>();
		this.maxWidth = 0;
		this.maxHeigh = 0;
		this.maxWidthLength=0;
		this.maxHeighLength=0;
		this.flag=flag;
	}

	/**
	 * 设置列宽最大值
	 * 
	 * @param obj
	 */
	public void updateAutoWidth(ExcelUnitObject obj) {
		if (autuWidthSettingMap == null) {
			autuWidthSettingMap = new HashMap<>();
		}
		/**
		 * 设置列高自适应值
		 */

		/**
		 * 如果>0，说明是合并单元格，则25%由自身决定
		 */
		Double valueWidth = (double) obj.getValueWidth();
		if (obj.getWidth() > 0) {
			valueWidth = (double) obj.getValueWidth() / (obj.getWidth() + 1);
		}
		if (autuWidthSettingMap.containsKey(obj.getX())) {
			Double integer = autuWidthSettingMap.get(obj.getX());
			if (integer != null && integer >= valueWidth) {
				return;
			}
		}
		autuWidthSettingMap.put(obj.getX(), valueWidth);
		if(valueWidth>maxWidthLength){
			maxWidthLength=valueWidth;
		}
	}

	/**
	 * 设置行高最大值
	 * 
	 * @param obj
	 * @throws CloneNotSupportedException
	 */
	public void updateAutoHeight(ExcelUnitObject obj)  {
		if (autuHeightSettingMap == null) {
			autuHeightSettingMap = new HashMap<>();
		}
		/**
		 * 设置行高自适应值
		 */
		Double valueHeight = (double) obj.getValueHeight();
		
		if (obj.getHigh() > 0) {
			/**
			 * 合并单元格权值:
			 * 首个单元格只占RATE%
			 */
			ExcelUnitObject clone = obj.clone();
			valueHeight *= rate;
			Double valueHeight2 = (double) clone.getValueHeight();
			valueHeight2 *= (1-rate);
			int high = clone.getHigh() - 1;
			int y = clone.getY() + 1;
			clone.setHigh(high);
			clone.setY(y);
			clone.setValueHeight(valueHeight2.intValue());
			updateAutoWidth(clone);
			
		}
		if (autuHeightSettingMap.containsKey(obj.getY())) {
			Double integer = autuHeightSettingMap.get(obj.getY());
			if (integer != null && integer >= valueHeight) {
				return;
			}
		}
		autuHeightSettingMap.put(obj.getY(), valueHeight);
		if(valueHeight>maxHeighLength){
			maxHeighLength=valueHeight;
		}
	}

	/**
	 * 增加一个注释
	 */
	public void addRemark(ExcelRemarkObject obj) {
		if (obj != null && !StringUtils.isEmpty(obj.getValue())) {
			remarks.add(obj);
		}
	}

	/**
	 * 增加一个单元格属性
	 * 
	 * @param obj
	 * @throws CloneNotSupportedException 
	 */
	public void addContent(ExcelUnitObject obj)  {

		if (obj.getX() >= 0 && obj.getY() >= 0 && obj.getHigh() >= 0 && obj.getWidth() >= 0) {
			this.content.add(obj);
			/**
			 * 更新最大高、宽值
			 */
			int wid = obj.getX() + obj.getWidth();
			int hig = obj.getY() + obj.getHigh();
			if (this.maxWidth < wid) {
				this.maxWidth = wid;
			}
			if (this.maxHeigh < hig) {
				this.maxHeigh = hig;
			}
			updateAutoWidth(obj);
			updateAutoHeight(obj);
		}
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public List<ExcelUnitObject> getContent() {
		return content;
	}

	public int getMaxWidth() {
		return maxWidth;
	}

	public int getMaxHeigh() {
		return maxHeigh;
	}

	public Map<Integer, Double> getAutuHeightSettingMap() {
		return autuHeightSettingMap;
	}

	public Map<Integer, Double> getAutuWidthSettingMap() {
		return autuWidthSettingMap;
	}

	public List<ExcelRemarkObject> getRemarks() {
		return remarks;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public double getMaxWidthLength() {
		return maxWidthLength;
	}

	public double getMaxHeighLength() {
		return maxHeighLength;
	}

	public void setRate(double rate) {
		this.rate = rate;
	}

	public void setMaxWidth(int maxWidth) {
		this.maxWidth = maxWidth;
	}

	public void setMaxHeigh(int maxHeigh) {
		this.maxHeigh = maxHeigh;
	}

}
